Feature/hierholzer_algorithm pr#392
Conversation
Add Hierholzer's algorithm (C++) to CPP/algorithms/graph_algorithms
🔍 Duplicate Detection Results
|
|
🎉 Welcome to Hacktoberfest 2025, @Abdullah-Shah-26! 🎃 Thank you for your first contribution to our DSA repository! Here's what happens next: 🔍 Automatic Checks
📋 Next Steps🎯 Great job! Your code compiled successfully. Maintainers @Karanjot786 and @Pradeepsingh61 will review your PR soon. 🎁 What You Get
💡 Tips for Success
Welcome to the community! 🚀 |
🤖 Automated PR Status🔍 Code Validation✅ Passed - File naming and structure look good! 🧪 Compilation Tests✅ Passed - All code compiles successfully! 📋 Overall Status🎉 Ready for Review - Your PR has passed all automated checks! This comment was generated automatically. Checks will re-run when you push new commits. |
Problem
Given a connected graph (directed or undirected), find:
Eulerian Path → visits every edge exactly once.
Eulerian Circuit → visits every edge exactly once and starts/ends at the same vertex.
Conditions
Undirected Graph
Eulerian Circuit → all vertices have even degree.
Eulerian Path → exactly 2 vertices have odd degree.
Directed Graph
Eulerian Circuit → indegree = outdegree for all vertices.
Eulerian Path → exactly one vertex has (out = in + 1), one has (in = out + 1), rest balanced.
Approach (Hierholzer’s Algorithm)
Choose a starting node:
Circuit → any node.
Path → vertex with (outdegree > indegree).
Traverse edges using a stack, removing them as you go.
When stuck, backtrack and add vertex to path.
Continue until all edges are used.
Reverse result → Eulerian Path / Circuit.
Visualization Example
Graph: 0→1, 1→2, 2→0, 0→3, 3→0
Walk: 0 → 3 → 0 → 1 → 2 → 0
Backtrack order: 0, 2, 1, 0, 3, 0
Final Path: 0 → 3 → 0 → 1 → 2 → 0 (Eulerian Circuit)
Complexity
Time: O(E) → each edge is used exactly once.
Space: O(V + E) → adjacency list + stack.